package com.tjsiqi.service;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.base.Preconditions;
import com.tjsiqi.beans.LogType;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.common.RequestHolder;
import com.tjsiqi.dao.*;
import com.tjsiqi.dto.SearchLogDto;
import com.tjsiqi.dto.SearchstRwdDto;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.model.*;
import com.tjsiqi.param.SearchLogParam;
import com.tjsiqi.param.SearchstRwdParam;
import com.tjsiqi.param.SearchwxUnitsParam;
import com.tjsiqi.util.BeanValidator;
import com.tjsiqi.util.IpUtil;
import com.tjsiqi.util.JsonMapper;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysStRwdService {

    @Resource
    private WxhntRWDMapper wxhntRWDMapper;
    //  查询任务单数据
    public PageResult<WxhntRWD> searchPageList(SearchstRwdParam param, PageQuery page) {
        BeanValidator.check(page);
        SearchstRwdDto dto = getSearchstRwdDto(param);
        System.out.println("----"+dto);
        int count = wxhntRWDMapper.countBySearchDto(dto);
        if (count > 0){
            List<WxhntRWD> stRwdList = wxhntRWDMapper.getPageListBySearchDto(dto, page);
            return PageResult.<WxhntRWD>builder().total(count).data(stRwdList).build();
        }
        return PageResult.<WxhntRWD>builder().build();
    }

    // 更新任务单
    public void acceptById(int id, String status) {
        wxhntRWDMapper.acceptById(id,status);
    }
    //  导出商砼任务单
    public  List<WxhntRWDEXP>  exportByParam(SearchstRwdParam param) {
        List<WxhntRWDEXP> stRwdList =new ArrayList<WxhntRWDEXP>();
        SearchstRwdDto dto = getSearchstRwdDto(param);
        int count = wxhntRWDMapper.countBySearchDto(dto);
        if (count > 0){
            stRwdList = wxhntRWDMapper.getListBySearchDto(dto);
            return stRwdList;
        }
        return stRwdList;
    }

// 删除
    public int removeByParam(SearchstRwdParam param) {
        SearchstRwdDto dto = getSearchstRwdDto(param);
        System.out.println("删除操作传来参数"+dto);
        int isRemove = wxhntRWDMapper.removeByParam(dto);
        return isRemove;
    }
//  封装查询参数
    public  SearchstRwdDto getSearchstRwdDto(SearchstRwdParam param){
        SearchstRwdDto dto = new SearchstRwdDto();
        dto.setType(param.getType());
        if (StringUtils.isNotBlank(param.getJbzCode())) {
            dto.setJbzCode(param.getJbzCode());
        }
        if (StringUtils.isNotBlank(param.getStUnit())) {
            dto.setStUnit("%" + param.getStUnit() + "%");
        }

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            if (StringUtils.isNotBlank(param.getFromTime())) {
                dto.setFromTime(dateFormat.parse(param.getFromTime()));
            }
            if (StringUtils.isNotBlank(param.getToTime())) {
                dto.setToTime(dateFormat.parse(param.getToTime()));
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题，正确格式为：yyyy-MM-dd");
        }

        return dto;
    }
}
